package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.system.domain.Project;
import com.ruoyi.system.service.IProjectService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ProjectProperty;
import com.ruoyi.system.service.IProjectPropertyService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 项目属性管理Controller
 *
 * @author peter
 * @date 2019-12-30
 */
@Controller
@RequestMapping("/system/property")
public class ProjectPropertyController extends BaseController {
    private String prefix = "system/property";

    @Autowired
    private IProjectPropertyService projectPropertyService;

    @Autowired
    private IProjectService projectService;

    @RequiresPermissions("system:property:view")
    @GetMapping()
    public String property(Model model) {
        List<Project> projects = projectService.selectProjectList(null);
        model.addAttribute("projects", projects);
        return prefix + "/property";
    }

    /**
     * 查询项目属性管理列表
     */
    @RequiresPermissions("system:property:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProjectProperty projectProperty) {
        startPage();

        List<ProjectProperty> list = projectPropertyService.selectProjectPropertyList(projectProperty);
        return getDataTable(list);
    }

    /**
     * 导出项目属性管理列表
     */
    @RequiresPermissions("system:property:export")
    @Log(title = "项目属性管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProjectProperty projectProperty) {
        List<ProjectProperty> list = projectPropertyService.selectProjectPropertyList(projectProperty);
        ExcelUtil<ProjectProperty> util = new ExcelUtil<ProjectProperty>(ProjectProperty.class);
        return util.exportExcel(list, "property");
    }

    /**
     * 新增项目属性管理
     */
    @GetMapping("/add")
    public String add(Model model) {
        List<Project> projects = projectService.selectProjectList(null);
        model.addAttribute("projects", projects);
        return prefix + "/add";
    }

    /**
     * 新增保存项目属性管理
     */
    @RequiresPermissions("system:property:add")
    @Log(title = "项目属性管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProjectProperty projectProperty) {
        return toAjax(projectPropertyService.insertProjectProperty(projectProperty));
    }

    /**
     * 修改项目属性管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {

        List<Project> projects = projectService.selectProjectList(null);
        mmap.put("projects", projects);
        ProjectProperty projectProperty = projectPropertyService.selectProjectPropertyById(id);
        mmap.put("projectProperty", projectProperty);
        return prefix + "/edit";
    }

    /**
     * 修改保存项目属性管理
     */
    @RequiresPermissions("system:property:edit")
    @Log(title = "项目属性管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProjectProperty projectProperty) {
        return toAjax(projectPropertyService.updateProjectProperty(projectProperty));
    }

    /**
     * 删除项目属性管理
     */
    @RequiresPermissions("system:property:remove")
    @Log(title = "项目属性管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(projectPropertyService.deleteProjectPropertyByIds(ids));
    }
}
